<template>
  <el-container class="container">
    <el-header>
      <div>
        <img src="../assets/logo.png" alt="" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 侧边栏 -->
        <div class="toggle-btn" @click="toggleCollapse()">|||</div>
        <el-menu
          :default-active="$route.path"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409eff"
          :collapse = "isCollapse"
          :collapse-transition = "false"
          unique-opened
          router
        >
          <!-- 一级菜单 -->
          <el-submenu v-for="item in menulist" :key="item.id" :index="item.id+''">
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item v-for="subItem in item.children"
            :key="subItem.id" :index="'/'+subItem.path"
            @click="saveNavState('/'+subItem.path)">
              <!-- 二级菜单模板区域 -->
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [], // 左侧菜单栏数据
      iconObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      isCollapse: false, // 菜单折叠 
      activePath: '', // 激活链接高亮
    };
  },
  created(){
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem('activePath');
  },
  methods: {
    // 获取左侧菜单栏数据
    getMenuList(){
      let that = this;
      that.$http.get('menus').then(res =>{
        if (res.data.meta.status !== 200) return that.$message.error(res.data.meta.msg);
        that.menulist = res.data.data;
        // console.log(that.menulist);
      })
    },
    // 菜单折叠按钮
    toggleCollapse(){
      this.isCollapse = !this.isCollapse;
    },
    // 保存跳转链接
    saveNavState(activePath){
      window.sessionStorage.setItem('activePath', activePath);
      this.activePath = activePath;
    },
    // 退出登录
    logout() {
      window.sessionStorage.clear();
      // window.sessionStorage.removeItem("token");
      this.$router.push("/login");
      this.$message({
        message: "退出登录",
        type: "info",
      });
    },
  },
};
</script>

<style lang="less" scoped>
.container {
  height: 100%;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-left: 0;
  color: #fff;
  font-size: 20px;

  div {
    display: flex;
    align-items: center;

    img {
      max-width: 30px;
      margin: 8px;
      padding: 8px;
      background-color: #eee;
      border-radius: 50%;
      overflow: hidden;
    }
  }
}

.el-aside {
  background-color: #333744;

  .toggle-btn{
    background: #4a5064;
    color: #fff;
    font-size: 10px;
    line-height: 24px;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }

  .el-menu{
    border-right: none;
  }
}

.el-main {
  background-color: #eaedf1;
}

.iconfont{
  margin-right: 10px;
}
</style>